package testLayer;

import static org.junit.Assert.*;

import java.sql.SQLException;

import modelLayer.ClassRoom;
import modelLayer.Subject;
import modelLayer.Teacher;
import modelLayer.Class;
import modelLayer.TeachingBlock;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import dbLayer.DBClass;
import dbLayer.DBClassRoom;
import dbLayer.DBSubject;
import dbLayer.DBTeacher;
import dbLayer.DBTeachingBlock;

public class TeachingBlockDatabaseTest {
	
	private DBTeachingBlock dbTeachingBlock = new DBTeachingBlock();
	private boolean retrieveAssociation = false;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		
		Teacher t = new DBTeacher().findById(1); 
		new DBClass().insert("men0813", "1", "2013", t);
		
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		
		DBClass dbCl = new DBClass();
		dbCl.remove(dbCl.find("men0813", false).getId());
		
	}

	@Before
	public void setUp() throws Exception {
		
		testInsert();
		
	}

	@After
	public void tearDown() throws Exception {
		
		testRemove();
		
	}

	@Test
	public void testDBTeachingBlock() {
		
		assertNotNull(this.dbTeachingBlock);
		
	}

	@Test
	public void testInsert() throws SQLException {
		
//		try {
//			Class c = new DBClass().find("men0813", false);
//			Subject s = new DBSubject().findById(1, false);
//			ClassRoom clRoom = new DBClassRoom().findById(2);
//			assertTrue(this.dbTeachingBlock.insert(c, s, clRoom, "xxxx") > 0);
//		}
//		catch (SQLException e) {
//			
//		}
		
	}

	@Test
	public void testFind() {
		
		assertTrue(this.dbTeachingBlock.find("xxxx", retrieveAssociation) != null);
		
	}

	@Test
	public void testFindById() {
		
		TeachingBlock tb = this.dbTeachingBlock.find("xxxx", false);
		assertTrue(this.dbTeachingBlock.findById(tb.getId(), retrieveAssociation) != null);
		
	}

	@Test
	public void testGetAll() {
		
		assertTrue(this.dbTeachingBlock.getAll(retrieveAssociation).size() > 0);
		
	}

	@Test
	public void testUpdate() throws SQLException {
		
		TeachingBlock tb = this.dbTeachingBlock.find("xxxx", false);
		ClassRoom cr = new DBClassRoom().findById(1);
		tb.setClassRoom(cr);
		assertTrue(this.dbTeachingBlock.update(tb) > 0);
		
	}

	@Test
	public void testRemove() {
		
		TeachingBlock tb = this.dbTeachingBlock.find("xxxx", false);
		try {
			assertTrue(this.dbTeachingBlock.remove(tb.getId()) > 0);
		}
		catch (Exception e) {
			
		}
		
	}

}
